package com.yuanli.cosmeticmirror.model.impl;

import android.text.TextUtils;
import android.util.Log;

import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.google.gson.Gson;
import com.rxjava.rxlife.BaseScope;
import com.rxjava.rxlife.RxLife;
import com.yuanli.cosmeticmirror.bean.BaseBean;
import com.yuanli.cosmeticmirror.bean.MemberBean;
import com.yuanli.cosmeticmirror.bean.PhotoFrame;
import com.yuanli.cosmeticmirror.bean.PhotoFrameBean;
import com.yuanli.cosmeticmirror.model.biz.IPhotoFrameBiz;

import java.util.List;

import rxhttp.wrapper.param.RxHttp;

import static com.yuanli.cosmeticmirror.base.HttpApi.GET_FRAME_TYPE;
import static com.yuanli.cosmeticmirror.base.HttpApi.GET_FRAME_TYPE_FOR_ID;
import static com.yuanli.cosmeticmirror.base.MyApp.TAG;

public class PhotoFrameImpl extends BaseScope implements IPhotoFrameBiz {
    public PhotoFrameImpl(LifecycleOwner owner) {
        super(owner);
    }

    @Override
    public void getPhotoFrame(CallbackListener<PhotoFrame> listener) {
        RxHttp.get(GET_FRAME_TYPE)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Log.d(TAG, "getPhotoFrame onSuccess: " + s);
                    PhotoFrame response = JSON.parseObject(s, PhotoFrame.class);
                    if (response != null) {
                        listener.success(response);
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    //请求失败
                    Log.d(TAG, "getPhotoFrame onFaild: " + throwable.getMessage());
                    listener.failed(throwable.getMessage());
                });
    }

    @Override
    public void getPhotoFrameForId(String id, CallbackListener<List<PhotoFrameBean>> listener) {
        RxHttp.get(GET_FRAME_TYPE_FOR_ID)
                .add("typeid", id)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Log.d(TAG, "getPhotoFrameForId onSuccess: " + s);
                    try {
                        JSONObject jsonObject = JSON.parseObject(s);
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        if (jsonArray == null) {
                            listener.failed(null);
                            return;
                        }
                        List<PhotoFrameBean> tutorialBean = JSON.parseObject(jsonArray.toJSONString(), new TypeReference<List<PhotoFrameBean>>() {
                        });
                        if (tutorialBean != null) {
                            listener.success(tutorialBean);
                        } else {
                            listener.failed(null);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }, throwable -> {
                    //请求失败
                    Log.d(TAG, "getPhotoFrameForId onFaild: " + throwable.toString());
                    listener.failed(throwable.getMessage());
                });
    }
}
